package com.hhkj.admin.order.controller;

import com.hhkj.admin.common.annotation.Idempotent;
import com.hhkj.admin.order.common.valid.ConfirmPayOrder;
import com.hhkj.admin.order.common.cont.OrderConst;
import com.hhkj.admin.order.domain.Order;
import com.hhkj.admin.order.model.req.OrderReq;
import com.hhkj.admin.order.model.res.OrderRes;
import com.hhkj.admin.order.service.biz.OrderBizService;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.v2.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import com.ruoyi.common.valid.VPage;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/order")
@PermissionPrefix(value = "course:order")
public class OrderController extends BaseController<Order, OrderReq, OrderRes, OrderBizService> {

    /** 订单导入模板下载 */
    @GetMapping("/import/template")
    public void downloadOrderImportTemplate(HttpServletResponse response) {
        ExcelUtils.downLoadExcel(OrderConst.ORDER_IMPORT_TEMPLATE_NAME, response, this.getBizService().createImportTemplate());
    }

    /** 订单导入 */
    @PostMapping("/import/template")
    @PreAuthorize("@ss.hasPermi(this, 'import')")
    public AjaxResult importOrder(@RequestParam("saleId") Long saleId, @RequestParam("file") MultipartFile file) {
        return AjaxResult.success(getBizService().validateAndCreateOrderByExcel(saleId, file, OrderConst.SourceTypeCode.SYSTEM, null));
    }

    /**
     * 后台程序，管理员手动确认付款
     *
     * @param orderReq
     * @return
     */
    @PutMapping("/system/pay/confirm")
    @PreAuthorize("@ss.hasPermi(this, 'pay')")
    public AjaxResult confirmPayByAdmin(@RequestBody @Validated(value = ConfirmPayOrder.class) OrderReq orderReq) {
        orderReq.setPayMethodCode(OrderConst.PayMethodEnum.OFFLINE.getCode());
        return AjaxResult.success(getBizService().validateAndPayOrder(orderReq, null));
    }

    /** 分佣数据导出 */
    @PostMapping("/export")
    public void export(@RequestBody OrderReq orderReq, HttpServletResponse response)
    {
        ExcelUtils.downLoadExcel(OrderConst.EXPORT_EXCEL_TITLE, response, this.getBizService().createExportWorkbook(orderReq));
    }

    /** 分页 */
    @Override
    @PostMapping("/paging")
    @PreAuthorize("@ss.hasPermi(this, 'page')")
    public TableDataInfo paging(@Validated(VPage.class) @RequestBody OrderReq orderReq) {
        return super.paging(orderReq);
    }

    @Override
    @GetMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPermi(this, 'view')")
    public AjaxResult getById(@Validated @NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return super.getById(id);
    }
}
